package com.steakliu.springbootwebsocket.component;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;

/**
 * 功能说明：
 * <p>
 * Original @Author: steakliu-刘牌, 2022-06-27  23:55
 * <p>
 * Copyright (C)2020-2022  小四的技术之旅 All rights reserved.
 */
@Component
@Slf4j
@ServerEndpoint("/websocket")
public class WebSocketServer {

    @OnOpen
    public void onOpen(Session session){
        try {
            log.info("建立链接【{}】",session);
        }catch (Exception e){
            log.info("连接异常【{}】",e.getMessage());
        }
    }

    @OnClose
    public void onClose(){
        log.info("=====websocket连接关闭======");
    }

    @OnMessage
    public String onMessage(String message , Session session){
        log.info("接收消息【{}】，session【{}】",message,session);
        return "接收到的消息：【"+message+"】";
    }

    @OnError
    public void onError(Session session, Throwable error){
        log.info("session【{}】",session);
        error.printStackTrace();
    }
}
